package com.gfilechooser;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.os.FileObserver;
import android.support.v4.content.AsyncTaskLoader;

import com.gfilechooser.utils.FileUtils;

/**
 * @author Guillermo
 */

public class FileLoader extends AsyncTaskLoader<List<File>> {

	private static final int FILE_OBSERVER_MASK = FileObserver.CREATE
			| FileObserver.DELETE | FileObserver.DELETE_SELF
			| FileObserver.MOVED_FROM | FileObserver.MOVED_TO
			| FileObserver.MODIFY | FileObserver.MOVE_SELF;

	private FileObserver fileObserver;

	private List<File> data;
	private String path;

	public FileLoader(Context context, String path) {
		super(context);
		this.path = path;
	}

	@Override
	public List<File> loadInBackground() {
		return FileUtils.getFileList(path);
	}

	@Override
	public void deliverResult(List<File> data) {
		if (isReset()) {
			onReleaseResources(data);
			return;
		}

		List<File> oldData = this.data;
		this.data = data;

		if (isStarted())
			super.deliverResult(data);

		if (oldData != null && oldData != data)
			onReleaseResources(oldData);
	}

	@Override
	protected void onStartLoading() {
		if (data != null)
			deliverResult(data);

		if (fileObserver == null) {
			fileObserver = new FileObserver(path, FILE_OBSERVER_MASK) {
				@Override
				public void onEvent(int event, String path) {
					onContentChanged();
				}
			};
		}
		fileObserver.startWatching();

		if (takeContentChanged() || data == null)
			forceLoad();
	}

	@Override
	protected void onStopLoading() {
		cancelLoad();
	}

	@Override
	protected void onReset() {
		onStopLoading();

		if (data != null) {
			onReleaseResources(data);
			data = null;
		}
	}

	@Override
	public void onCanceled(List<File> data) {
		super.onCanceled(data);

		onReleaseResources(data);
	}

	protected void onReleaseResources(List<File> data) {

		if (fileObserver != null) {
			fileObserver.stopWatching();
			fileObserver = null;
		}
	}
}